package indi.zhifa.recipe.bailan.framework.enums.service.impl;
import indi.zhifa.recipe.bailan.framework.common.entity.exception.ServiceException;
import indi.zhifa.recipe.bailan.framework.enums.entity.po.SysTypeEntity;
import indi.zhifa.recipe.bailan.framework.enums.entity.vo.SysTypeVo;
import indi.zhifa.recipe.bailan.framework.enums.service.ISysTypeMemo;
import indi.zhifa.recipe.bailan.framework.redis.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SysTypeMemoImpl implements ISysTypeMemo {

    private final String TYPE_MGR_PREFIX = "TYPE_MGR:";
    private final String TYPE_MGR_CODE_PREFIX = "TYPE_MGR_CODE:";
    private final RedisUtil mRedisUtil;

    public void memo(SysTypeVo pSysTypeVo){
        mRedisUtil.set(TYPE_MGR_PREFIX+pSysTypeVo.getId(),pSysTypeVo);
        mRedisUtil.set(TYPE_MGR_CODE_PREFIX+pSysTypeVo.getCode(),pSysTypeVo.getId());
    }

    public void delete(SysTypeEntity pSysTypeVo){
        mRedisUtil.delete(TYPE_MGR_PREFIX+pSysTypeVo.getId());
        mRedisUtil.delete(TYPE_MGR_CODE_PREFIX+pSysTypeVo.getCode());
    }

    public SysTypeVo infoFromMemo(long pId){
        SysTypeVo sysTypeVo = mRedisUtil.get(TYPE_MGR_PREFIX+pId,SysTypeVo.class);
        return sysTypeVo;
    }

    public SysTypeVo infoFromMemo(String pCode){
        Long id = mRedisUtil.get(TYPE_MGR_CODE_PREFIX+pCode,Long.class);
        if(null == id){
            return null;
        }
        return infoFromMemo(id);
    }
}
